import pandas as pd
from sklearn.preprocessing import StandardScaler
import joblib

# 假设你知道特征的数量
num_features = 78  # 这里填写你的特征数量

# 1. 加载数据，假设没有列名
data = pd.read_csv('expendData\\total_extend.csv', header=None)

# 2. 手动设置列名，假设最后一列是'Label'，其余为'feature_1', 'feature_2', ...
columns = [
    'Destination Port', 'Flow Duration', 'Total Fwd Packets', 'Total Backward Packets',
    'Total Length of Fwd Packets', 'Total Length of Bwd Packets', 'Fwd Packet Length Max',
    'Fwd Packet Length Min', 'Fwd Packet Length Mean', 'Fwd Packet Length Std',
    'Bwd Packet Length Max', 'Bwd Packet Length Min', 'Bwd Packet Length Mean',
    'Bwd Packet Length Std', 'Flow Bytes/s', 'Flow Packets/s', 'Flow IAT Mean',
    'Flow IAT Std', 'Flow IAT Max', 'Flow IAT Min', 'Fwd IAT Total', 'Fwd IAT Mean',
    'Fwd IAT Std', 'Fwd IAT Max', 'Fwd IAT Min', 'Bwd IAT Total', 'Bwd IAT Mean',
    'Bwd IAT Std', 'Bwd IAT Max', 'Bwd IAT Min', 'Fwd PSH Flags', 'Bwd PSH Flags',
    'Fwd URG Flags', 'Bwd URG Flags', 'Fwd Header Length', 'Bwd Header Length',
    'Fwd Packets/s', 'Bwd Packets/s', 'Min Packet Length', 'Max Packet Length',
    'Packet Length Mean', 'Packet Length Std', 'Packet Length Variance',
    'FIN Flag Count', 'SYN Flag Count', 'RST Flag Count', 'PSH Flag Count',
    'ACK Flag Count', 'URG Flag Count', 'CWE Flag Count', 'ECE Flag Count',
    'Down/Up Ratio', 'Average Packet Size', 'Avg Fwd Segment Size',
    'Avg Bwd Segment Size', 'Fwd Header Length', 'Fwd Avg Bytes/Bulk',
    'Fwd Avg Packets/Bulk', 'Fwd Avg Bulk Rate', 'Bwd Avg Bytes/Bulk',
    'Bwd Avg Packets/Bulk', 'Bwd Avg Bulk Rate', 'Subflow Fwd Packets',
    'Subflow Fwd Bytes', 'Subflow Bwd Packets', 'Subflow Bwd Bytes',
    'Init_Win_bytes_forward', 'Init_Win_bytes_backward', 'act_data_pkt_fwd',
    'min_seg_size_forward', 'Active Mean', 'Active Std', 'Active Max',
    'Active Min', 'Idle Mean', 'Idle Std', 'Idle Max', 'Idle Min', 'Label'
]
data.columns = columns

# 3. 分离特征和目标变量
X = data.drop('Label', axis=1)  # 特征
y = data['Label']  # 目标变量

# 4. 应用标准化
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

# 5. 保存scaler对象
joblib.dump(scaler, 'scaler.joblib')

# 在后续的预测中，加载scaler并对新数据进行相同的处理
# 加载scaler
#loaded_scaler = joblib.load('scaler.joblib')

# 加载新的数据（同样假设没有列名）
#new_data = pd.read_csv('new_data.csv', header=None)

# 由于新数据的结构应该与训练数据相同，我们可以直接使用相同的列名
#new_data.columns = columns

# 分离新数据的特征
#new_X = new_data.drop('Label', axis=1)

# 使用加载的scaler对新数据进行标准化
#new_X_scaled = loaded_scaler.transform(new_X)

# 现在new_X_scaled可以被模型用来进行预测
# predictions = model.predict(new_X_scaled)